package cn.edu.hust.cm.lib;

/**
 * 测试程序运行时间
 * 
 * @author 陈明
 * 
 */
public abstract class TestClock {

	@Override
	public String toString() {
		return "TestClock [testName=" + testName + ", start=" + start
				+ ", end=" + end + ", loops=" + loops + ", times=" + times
				+ ", use-time=" + (end - start) / 1000.0 + "]";
	}

	/**
	 * 循环次数
	 */
	public static final int LOOPS = 10000;
	/**
	 * 测试次数
	 */
	public static final int TIMES = 10;
	/**
	 * 当前测试的标识符
	 */
	protected String testName;
	/**
	 * 当前测试的开始时间
	 */
	protected long start;
	/**
	 * 当前测试的结束时间
	 */
	protected long end;

	protected int times = TIMES;
	protected int loops = LOOPS;

	public TestClock runTest(String testName, int times, int loops)
			throws Exception {
		this.testName = testName;
		this.times = times;
		this.loops = loops;
		start = System.currentTimeMillis();
		for (int i = 0; i < times; i++) {
			for (int j = 0; j < loops; j++) {
				test();
			}
		}
		end = System.currentTimeMillis();
		return this;
	}

	public TestClock runTest(String testName) throws Exception {
		return runTest(testName, TIMES, LOOPS);
	}

	/**
	 * 具体测试过程
	 */
	protected abstract void test() throws Exception;
}
